<?php

namespace addons\mental\controller;

use think\Hook;
use app\common\library\Sms as smsLib;
use addons\mental\model\User;

class Sms extends Base
{

    /**
     * 发送验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     */
    public function send()
    {
        $mobile = $this->request->param("mobile");
        $event = $this->request->param("event");
        $event = $event ? $event : 'register';

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile phone number is not correct'));
        }
        $last = smsLib::get($mobile, $event);
        if ($last && time() - $last['createtime'] < 60) {
            $this->error(__('Sending frequent'));
        }
        $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        if ($ipSendTotal >= 5) {
            $this->error(__('Sending frequent'));
        }
        if ($event) {
            $userinfo = User::getByMobile($mobile);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('Has already been registered'));
            } elseif (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                $this->error(__('Has been occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('unregistered'));
            }
        }
        if (!Hook::get('sms_send')) {
            $this->error(__('Please installation sms validation plug-in plug-in management in the background'));
        }
        $ret = smsLib::send($mobile, null, $event);
        if ($ret) {
            $this->success(__('Send a success'));
        } else {
            $this->error(__('Send failure'));
        }
    }

    /**
     * 检测验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $mobile = $this->request->param("mobile");
        $event = $this->request->param("event");
        $event = $event ? $event : 'register';
        $captcha = $this->request->param("captcha");

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile phone number is not correct'));
        }
        if ($event) {
            $userinfo = User::getByMobile($mobile);
            if ($event == 'register' && $userinfo) {
                //已被注册
                $this->error(__('Has already been registered'));
            } elseif (in_array($event, ['changemobile']) && $userinfo) {
                //被占用
                $this->error(__('Has been occupied'));
            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
                //未注册
                $this->error(__('unregistered'));
            }
        }
        $ret = smsLib::check($mobile, $captcha, $event);
        if ($ret) {
            $this->success(__('Operation completed'));
        } else {
            $this->error(__('Verification code is not correct'));
        }
    }
}
